2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex5_masterMultipleSlave.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //******************************************************************************
35 // MSP430FR57xx Demo - USCI_B0 I2C Master TX bytes to Multiple Slaves
36 //
37 // Description: This demo connects two MSP430's via the I2C bus.
38 // The master transmits to 4 different I2C slave addresses 0x0A,0x0B,0x0C&0x0D.
39 // Each slave address has a specific related data in the array TXData[].
40 // At the end of four I2C transactions the slave address rolls over and begins
41 // again at 0x0A.
42 // ACLK = n/a, MCLK = SMCLK = BRCLK = default DCO = ~1.045MHz
43 // Use with MSP430FR57xx_uscib0_i2c_MultiSlave.c
44 //
45 // /|\ /|\
46 // MSP430FR5739 10k 10k MSP430FR5739
47 // slave | | master
48 // ----------------- | | -----------------
49 // -|XIN P1.6/UCB0SDA|<-|----+->|P1.6/UCB0SDA XIN|-
50 // | | | | |
51 // -|XOUT | | | XOUT|-
52 // | P1.7/UCB0SCL|<-+------>|P1.7/UCB0SCL |
53 // | | | |
54 //
55 //******************************************************************************
56 
57 
58 uint8_t TXData[]= {0xA1,0xB1,0xC1,0xD1};// Pointer to TX data
59 uint8_t SlaveAddress[]= {0x0A,0x0B,0x0C,0x0D};
60 uint8_t TXByteCtr;
61 uint8_t SlaveFlag = 0;
62 
63 void main(void)
64 {
65  WDT_A_hold(WDT_A_BASE);
66 
67  // Configure MCLK=SMCLK=ACLK=DCOCLK for 1MHz operation
68  //Set DCO frequency to 8MHz
69  CS_setDCOFreq(CS_DCORSEL_0,CS_DCOFSEL_3);
70  //Set ACLK = DCO with frequency divider of 8
71  CS_initClockSignal(CS_ACLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_8);
72  //Set SMCLK = DCO with frequency divider of 8
73  CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_8);
74  //Set MCLK = DCO with frequency divider of 8
75  CS_initClockSignal(CS_MCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_8);
76 
77 
78  // Configure Pins for I2C
79  //Set P1.6 and P1.7 as Secondary Module Function Input.
80  /*
81 
82  * Select Port 1
83  * Set Pin 6, 7 to input Secondary Module Function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
84  */
85  GPIO_setAsPeripheralModuleFunctionInputPin(
86  GPIO_PORT_P1,
87  GPIO_PIN6 + GPIO_PIN7,
88  GPIO_SECONDARY_MODULE_FUNCTION
89  );
90 
91  EUSCI_B_I2C_initMasterParam param = {0};
92  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
93  param.i2cClk = CS_getSMCLK();
94  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
95  param.byteCounterThreshold = 0;
96  param.autoSTOPGeneration = EUSCI_B_I2C_NO_AUTO_STOP;
97  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
98 
99 
100  //Set Master in receive mode
101  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
102  EUSCI_B_I2C_TRANSMIT_MODE
103  );
104  //Enable I2C Module to start operations
105  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
106 
107  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
108  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
109  EUSCI_B_I2C_NAK_INTERRUPT
110  );
111 
112  //Enable master Receive interrupt
113  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
114  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
115  EUSCI_B_I2C_NAK_INTERRUPT
116  );
117 
118  SlaveFlag =0;
119  while(1)
120  {
121  //Specify slave address
122  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
124  );
125 
126 
127  TXByteCtr = 1; // Load TX byte counter
128 
129  while (EUSCI_B_I2C_SENDING_STOP == EUSCI_B_I2C_masterIsStopSent
130  (EUSCI_B0_BASE));
131 
132 
133  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
134 
135  __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
136  // Remain in LPM0 until all data
137  // is TX'd
138  // Change Slave address
139  SlaveFlag++;
140  if (SlaveFlag>3) // Roll over slave address
141  {
142  SlaveFlag =0;
143  __delay_cycles(1000); // Delay between transmissions
144  }
145 
146  }
147 
148 }
149 
150 
151 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
152 #pragma vector=USCI_B0_VECTOR
153 __interrupt
154 #elif defined(__GNUC__)
155 __attribute__((interrupt(USCI_B0_VECTOR)))
156 #endif
157 void USCIB0_ISR(void)
158 {
159  switch(__even_in_range(UCB0IV,0x1E))
160  {
161  case 0x00: break; // Vector 0: No interrupts break;
162  case 0x02: break;
163  case 0x04:
164  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
165  break; // Vector 4: NACKIFG break;
166  case 0x06: break; // Vector 6: STTIFG break;
167  case 0x08: break; // Vector 8: STPIFG break;
168  case 0x0a: break; // Vector 10: RXIFG3 break;
169  case 0x0c: break; // Vector 14: TXIFG3 break;
170  case 0x0e: break; // Vector 16: RXIFG2 break;
171  case 0x10: break; // Vector 18: TXIFG2 break;
172  case 0x12: break; // Vector 20: RXIFG1 break;
173  case 0x14: break; // Vector 22: TXIFG1 break;
174  case 0x16: break; // Vector 24: RXIFG0 break;
175  case 0x18:
176  if (TXByteCtr) // Check TX byte counter
177  {
178  EUSCI_B_I2C_masterSendMultiByteNext(EUSCI_B0_BASE,
179  TXData[SlaveFlag]);
180  TXByteCtr--; // Decrement TX byte counter
181  }
182  else
183  {
184  EUSCI_B_I2C_masterSendMultiByteStop(EUSCI_B0_BASE);
185  __bic_SR_register_on_exit(CPUOFF);// Exit LPM0
186  }
187  break; // Vector 26: TXIFG0 break;
188  default: break;
189  }
190 }
191 
MPU_initThreeSegmentsParam param
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)